package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.model.net.TokenBean;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.UserHomePage;
import com.ttai.ui.activity.VerifyLogin;
import com.ttai.ui.activity.VerifyMiYao;

import retrofit2.Call;

public class VerifyAccountPresenter extends BasePresenter {
     VerifyLogin verifyLogin;



    public VerifyAccountPresenter(VerifyLogin verifyLogin) {
        this.verifyLogin = verifyLogin;
    }

    @Override
    protected void showError(String message) {
        Toast.makeText(verifyLogin,"验证失败",Toast.LENGTH_SHORT).show();
        verifyLogin.finish();
        Intent intent = new Intent(verifyLogin, EnterPage.class);
        verifyLogin.startActivity(intent);
    }

    @Override
    protected void parseJson(JsonObject json) {
        Toast.makeText(verifyLogin,"验证成功",Toast.LENGTH_SHORT).show();


        verifyLogin.finish();
//        System.out.println("myFlag:"+verifyLogin.flag);
        if (verifyLogin.flag.equals("1")){
            Intent intent = new Intent(verifyLogin, VerifyMiYao.class);
            verifyLogin.startActivity(intent);
        }else if (verifyLogin.flag.equals("2")){
            Gson gson = new Gson();
            TokenBean tokenBean = gson.fromJson(json,TokenBean.class);
            insertTokenDB(tokenBean.getToken());
//            recoverDB();
            Intent intent = new Intent(verifyLogin, UserHomePage.class);
            verifyLogin.startActivity(intent);
        }
    }

    private void insertTokenDB(String token) {
        DBHelper dbHelper = DBHelper.getInstance(verifyLogin);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("token",token);
        db.update("tokenacount",values, null, null);
    }

    public void verifyLogin(String flag,String data,String token,String cid){
        Call<ResponseInfo> verifyLoginToken = responseInfoApi.verifyLogin(flag, data, token,cid);
        verifyLoginToken.enqueue(new CallBackAdapter());
    }

    /**
     * 恢复本地数据库
     */
//    private void recoverDB() {
//        DBHelper dbHelper = DBHelper.getInstance(verifyLogin);
//        SQLiteDatabase sql=dbHelper.getWritableDatabase();
//        Cursor cursorToken=sql.rawQuery("select * from tokenacount",null);
//        while (cursorToken.moveToNext()){
//            myToken=cursorToken.getString(cursorToken.getColumnIndex("token"));
//        }
//        cursorToken.close();
//
//        Cursor cursorNewAcount=sql.rawQuery("select phone from newacount",null);
//        while (cursorNewAcount.moveToNext()){
//            newAcountValue=cursorNewAcount.getString(cursorNewAcount.getColumnIndex("phone"));
//        }
//        cursorNewAcount.close();
//
//        Cursor cursorTipAcount=sql.rawQuery("select phone from tipacount",null);
//        while (cursorTipAcount.moveToNext()){
//            tipAcountValue=cursorTipAcount.getString(cursorTipAcount.getColumnIndex("phone"));
//        }
//        cursorTipAcount.close();
//
//        Cursor cursorIdAcount=sql.rawQuery("select phone from idacount",null);
//
//        while (cursorIdAcount.moveToNext()){
//            idAcountValue=cursorIdAcount.getString(cursorIdAcount.getColumnIndex("phone"));
//        }
//        cursorIdAcount.close();
//
//        if (newAcountValue==null || newAcountValue.isEmpty() || !newAcountValue.equals(EnterPage.enterphonenumber)){
////            Thread newThread=new Thread(new NewAcountRunable());
////            newThread.start();
////            newAcountHandler=new NewAcountHandler();
//            verifyLogin.storeNewAcount();
////            System.out.println("newAcount为空");
//        }
//        if (tipAcountValue==null || tipAcountValue.isEmpty() || !tipAcountValue.equals(EnterPage.enterphonenumber)){
////            Thread tipThread=new Thread(new TipAcountRunable());
////            tipThread.start();
////            tipAcountHandler=new TipAcountHandler();
//            verifyLogin.storeTipAcount();
////            System.out.println("tipAcount为空");
//        }
//        if (idAcountValue==null || idAcountValue.isEmpty() || !idAcountValue.equals(EnterPage.enterphonenumber)){
////            Thread idThread=new Thread(new IdAcountRunable());
////            idThread.start();
////            idAcountHandler=new IdAcountHandler();
//            verifyLogin.restoreIdAcount();
////            System.out.println("idAcount为空");
//        }
//    }
}


